/* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hiof.moraa.server.service;

import com.hiof.moraa.server.service.model.LiveMediaFeed;
import org.imgscalr.Scalr;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import java.awt.image.BufferedImage;
import java.awt.image.ImagingOpException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Bergy
 */
public abstract class LiveMediaService {
    public static final int ICON_SIZE = 32;
    public static final int THUMBNAIL_SIZE = 150;
    FilePathService filePathService;

    protected FilePathService getFilePathService() {
        return filePathService;
    }

    public void setFilePathService(FilePathService filePathService) {
        this.filePathService = filePathService;
    }

    public abstract LiveMediaFeed processLiveMedia(int userId, int athleteId, MultipartFile file, double lat, double lng, double height, ServletContext servletContext);

    protected void writeFile(String filePath, MultipartFile originalFile) throws IOException {
        byte[] readData = new byte[1024];
        File file = new File(filePath);
        File parentDir = file.getParentFile();

        if(!parentDir.exists()){
            parentDir.mkdirs();
        }

        if (!file.exists()) {
            file.createNewFile();
        }

        InputStream fis = originalFile.getInputStream();

        BufferedImage scaledImage = Scalr.resize(ImageIO.read(fis), 448);
        ImageIO.write(scaledImage, "jpg", file);
        writeThumbnailImage(filePath);
        writeIconImage(filePath);
    }

    private void writeIconImage(String filePath)  throws ImagingOpException, IOException, IllegalArgumentException {
        BufferedImage thumbnail = Scalr.resize(ImageIO.read(new File(filePath)), ICON_SIZE);
        ImageIO.write(thumbnail, "jpg", new File(getIconPath(filePath)));
    }

    private String getIconPath(String relativeFilePath) {
        int mid = relativeFilePath.lastIndexOf(".");
        String frame = relativeFilePath.substring(0, mid);
        String ext = relativeFilePath.substring(mid + 1, relativeFilePath.length());
        return frame + "_icon." + ext;
    }

    private void writeThumbnailImage(String filePath) throws ImagingOpException, IOException, IllegalArgumentException {
        BufferedImage thumbnail = Scalr.resize(ImageIO.read(new File(filePath)), THUMBNAIL_SIZE);
        ImageIO.write(thumbnail, "jpg", new File(getThumbnailPath(filePath)));
    }

    private String getThumbnailPath(String relativeFilePath) {
        int mid = relativeFilePath.lastIndexOf(".");
        String frame = relativeFilePath.substring(0, mid);
        String ext = relativeFilePath.substring(mid + 1, relativeFilePath.length());
        return frame + "_thumb." + ext;
    }

    protected String getRelativeFilePath(String username, String routeName, MultipartFile file) {
        //TODO: Go to database get and fill: resources/img/user/[username]/[routename]/ for relative path
        //TODO: If file exists --> do like window with suffix _1+
        String path = "resources/img/user/" + username + "/" + routeName + "/";
        return (path +  file.getOriginalFilename()).toLowerCase();
    }
}
